package agentspeakide.editors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
import org.eclipse.jface.text.source.projection.ProjectionSupport;
import org.eclipse.jface.text.source.projection.ProjectionViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.VerifyKeyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.editors.text.TextEditor;

public class ASLEditor extends TextEditor {

	private ColorManager colorManager;
	private ProjectionSupport projectionSupport;
	private ProjectionAnnotationModel annotationModel;
	private Annotation[] oldAnnotations;

	public ASLEditor() {
		super();
		colorManager = new ColorManager();
		setSourceViewerConfiguration(new ASLConfiguration(colorManager, this));
		setDocumentProvider(new ASLDocumentProvider());
	}
	public void dispose() {
		colorManager.dispose();
		super.dispose();
	}
	
	public void createPartControl(Composite parent){
		super.createPartControl(parent);
		final ProjectionViewer viewer = (ProjectionViewer) getSourceViewer();
		projectionSupport = new ProjectionSupport(viewer, getAnnotationAccess(), getSharedColors());
		projectionSupport.install();
		
		viewer.doOperation(ProjectionViewer.TOGGLE);
		annotationModel = viewer.getProjectionAnnotationModel();
		
		viewer.appendVerifyKeyListener(
				   new VerifyKeyListener() {
				      public void verifyKey(VerifyEvent event) {

				      // Check for Ctrl+Spacebar
				      if (event.stateMask == SWT.CTRL && event.character == ' ') {

				        // Check if source viewer is able to perform operation
				        if (viewer.canDoOperation(SourceViewer.CONTENTASSIST_PROPOSALS))

				          // Perform operation
				          viewer.doOperation(SourceViewer.CONTENTASSIST_PROPOSALS);

				        // Veto this key press to avoid further processing
				        event.doit = false;
				      }
				   }
				});
	}
	
	protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler, int styles){
		ISourceViewer viewer = new ProjectionViewer(parent, ruler, getOverviewRuler(), isOverviewRulerVisible()
				, styles);
		getSourceViewerDecorationSupport(viewer);
		return viewer;
	}
	
	public void updateFoldingStructure(ArrayList<Position> positions)
	{
		Annotation[] annotations = new Annotation[positions.size()];
		
		HashMap<ProjectionAnnotation, Position> newAnnotations = new HashMap<ProjectionAnnotation, Position>();
		
		for(int i =0;i<positions.size();i++)
		{
			ProjectionAnnotation annotation = new ProjectionAnnotation();
			
			newAnnotations.put(annotation,positions.get(i));
			
			annotations[i] = (Annotation) annotation;
		}
		
		annotationModel.modifyAnnotations(oldAnnotations,(Map<ProjectionAnnotation, Position>)newAnnotations,null);
		
		oldAnnotations=annotations;
	}

}
